//
//  PlaceOrderViewController.m
//  ServiceDemo
//
//  Created by apple on 16/2/16.
//  Copyright (c) 2016年 HuaZhengInfo. All rights reserved.
//

#import "PlaceOrderViewController.h"
#import "ZLPhotoPickerViewController.h"
#import "ZLPhotoAssets.h"
#import "GYLAddImage.h"
#import "GetWGHModel.h"
#import "GrabOrderViewController.h"

@interface PlaceOrderViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIAlertViewDelegate,UITextViewDelegate>{

    UIScrollView * _mainView;
    UIImageView * _view1;
    UIImageView * _view2;
    UILabel * _roomLabel; // 区位
    UIButton * _cateButton; // 类型
    UILabel * _descLabel;
    UITextField * _textFiled; // 描述
    UITableView * _roomTableView; // 区位列表
    UITableView * _cateTableView; // 类型列表
    UILabel *_label4;
    GYLAddImage * _addImageView; // 添加图片
    XYTextView * _textView; // 描述
    UIButton *_grabBtn;
    
    NSMutableArray * _roomArray;
    NSMutableArray * _cateArray;
    NSMutableArray * _dataArray;
    NSString * _state; // 状态
    NSString * _ID; // ID
    NSString * _imageState; // 图片返回状态
    
    BOOL roomShow;
    BOOL cateShow;
}

@end

@implementation PlaceOrderViewController

- (void)viewWillAppear:(BOOL)animated{

    [_textFiled resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 初始化
    _cateArray = [NSMutableArray array];
    _roomArray = [NSMutableArray array];
    _dataArray = [NSMutableArray array];
    _state = @"SUCCESS";
    _ID = [[NSString alloc] init];
    roomShow = NO;
    cateShow = NO;
    [self buildMainUI];
    
    //  请求区位数据
    [self requestWGHData];
    
    //  请求订单类型数据
    [self requestSERVICEData];

    //  我要下单数据操作
//    [self orderSendData];

    
    
}


#pragma mark -- 创建UI
- (void)buildMainUI{

//    _mainView = [GDFactory creatUIViewWithFrame:CGRectMake(0, 0, SCR_W, SCR_H) backColor:RGBA(235, 235, 235, 1.0)];
    _mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H)];
    _mainView.backgroundColor = RGBA(235, 235, 235, 1.0);
    [self.view addSubview:_mainView];

    
    UILabel * Label1 = [GDFactory creatUILabelWithFrame:CGRectMake(10, 10, 100, 30) text:@"订单区位:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
    if (iPhone4) {
        Label1.frame = CGRectMake(10, 10, 100, 10);
    }else{
    
        Label1.frame = CGRectMake(10, 10, 100, 30);
    }
    [_mainView addSubview:Label1];
    
    _view1 = [GDFactory creatUIImageViewWithFrame:CGRectMake(10, CGRectGetMaxY(Label1.frame), SCR_W - 20, SCR_H/13) backgroundImage:@"下拉框_03"];
    if (iPhone4) {
        
        _view1.frame = CGRectMake(10, CGRectGetMaxY(Label1.frame)+ 5 , SCR_W - 20, SCR_H/13);
    }else{
    
        _view1.frame = CGRectMake(10, CGRectGetMaxY(Label1.frame), SCR_W - 20, SCR_H/13);
    }
//    [_view1.layer setBorderWidth:1.0];
//    [_view1.layer setBorderColor:RGBA(202, 202, 202, 1.0).CGColor];
    [_mainView addSubview:_view1];
    
    //订单区位
    _roomLabel = [GDFactory creatUILabelWithFrame:CGRectMake(10, 0, _view1.frame.size.width, _view1.frame.size.height) text:@"请选择" textColor:RGBA(164, 164, 164, 1.0) textFont:[UIFont systemFontOfSize:18]];
    _roomLabel.textAlignment = NSTextAlignmentLeft;
    
    // 添加点击手势
    UITapGestureRecognizer * roomGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(roomGseture)];
    _view1.userInteractionEnabled = YES;
    _roomLabel.userInteractionEnabled = YES;
    [_roomLabel addGestureRecognizer:roomGesture];
    
    [_view1 addSubview:_roomLabel];
    
    UILabel * label2 = [GDFactory creatUILabelWithFrame:CGRectMake(10, CGRectGetMaxY(_view1.frame), 100, 30) text:@"订单类型:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
    if (iPhone4) {
        label2.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame)+5, 100, 10);
    }else{
        label2.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame), 100, 30);
    
    }
    [_mainView addSubview:label2];
    

    _view2 = [GDFactory creatUIImageViewWithFrame:CGRectMake(10, CGRectGetMaxY(label2.frame), SCR_W - 20, SCR_H/12) backgroundImage:@"下拉框_03"];
    if (iPhone4) {
        _view2.frame = CGRectMake(10, CGRectGetMaxY(label2.frame) + 5, SCR_W - 20, SCR_H/13);
    }else{
    
        _view2.frame = CGRectMake(10, CGRectGetMaxY(label2.frame), SCR_W - 20, SCR_H/13);
    }
//    [_view2.layer setBorderWidth:1.0];
//    [_view2.layer setBorderColor:RGBA(202, 202, 202, 1.0).CGColor];
    _view2.userInteractionEnabled = YES;
    [_mainView addSubview:_view2];
    
    // 订单类型

    _cateButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, _view2.frame.size.width, _view2.frame.size.height)];
    [_cateButton setTitle:@"请选择" forState:UIControlStateNormal];
    [_cateButton setTitleColor:RGBA(164, 164, 164, 1.0) forState:UIControlStateNormal];
    _cateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _cateButton.titleLabel.font = [UIFont systemFontOfSize:18];
    
    [_cateButton addTarget:self action:@selector(cateGesture) forControlEvents:UIControlEventTouchUpInside];
    [_view2 addSubview:_cateButton];
    
//    _cateLabel = [GDFactory creatUILabelWithFrame:CGRectMake(10, 0, view2.frame.size.width, view2.frame.size.height) text:@"秩序类订单" textColor:RGBA(164, 164, 164, 1.0) textFont:[UIFont systemFontOfSize:18]];
////    _cateLabel.backgroundColor = [UI/Color redColor];
//    _cateLabel.textAlignment = NSTextAlignmentLeft;
//    // 添加点击手势
//    UITapGestureRecognizer * cateGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cateGesture)];
//    _descLabel.userInteractionEnabled = YES;
//    [_descLabel addGestureRecognizer:cateGesture];
//    
//    
//    [view2 addSubview:_cateLabel];
    
    UILabel * label3 = [GDFactory creatUILabelWithFrame:CGRectMake(10, CGRectGetMaxY(_view2.frame), 100, 30) text:@"问题描述:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
    if (iPhone4) {
        label3.frame = CGRectMake(10, CGRectGetMaxY(_view2.frame) + 5, 100, 10);
    }else{
    
        label3.frame = CGRectMake(10, CGRectGetMaxY(_view2.frame), 100, 30);
    }
    [_mainView addSubview:label3];
    
    
    // 问题描述
    
    _textView = [[XYTextView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(label3.frame), SCR_W - 20, SCR_H/8)];
    if (iPhone4) {
        _textView.frame = CGRectMake(10, CGRectGetMaxY(label3.frame) + 5, SCR_W - 20, SCR_H/8);
    }else{
    
        _textView.frame = CGRectMake(10, CGRectGetMaxY(label3.frame), SCR_W - 20, SCR_H/8);
    }
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.font = [UIFont systemFontOfSize:16.0];
    _textView.textColor = [UIColor blackColor];
    _textView.textAlignment = NSTextAlignmentLeft;
    //自定义键盘上面完成toolbar
    UIToolbar *customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0,0,SCR_W,40}];
    customAccessoryView.barTintColor = [UIColor whiteColor];
    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.frame = CGRectMake(0, 0, 40, 40);
    [sendButton setTitle:@"完成" forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(doneClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *finish = [[UIBarButtonItem alloc] initWithCustomView:sendButton];
    [customAccessoryView setItems:@[space,space,finish]];
    _textView.inputAccessoryView = customAccessoryView;
    
    [_mainView addSubview:_textView];
    
//    _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, view3.frame.size.width, view3.frame.size.height)];
//    _textFiled.placeholder = @"问题描述";
//    _textFiled.clearsOnBeginEditing = YES;
//    _textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
//    [_textFiled becomeFirstResponder];
//    [view3 addSubview:_textFiled];
    
//    _descLabel = [GDFactory creatUILabelWithFrame:CGRectMake(10, 0, view3.frame.size.width, view3.frame.size.height) text:@"B区地下室标识缺失" textColor:RGBA(164, 164, 164, 1.0) textFont:[UIFont systemFontOfSize:18]];
//    _descLabel.numberOfLines = 1;
//    [view3 addSubview:_descLabel];
    
    // 资料
    _label4 = [GDFactory creatUILabelWithFrame:CGRectMake(10, CGRectGetMaxY(_textView.frame), 100, 30) text:@"资料:" textColor:RED textFont:[UIFont systemFontOfSize:16]];
    if (iPhone4) {
        _label4.frame = CGRectMake(10, CGRectGetMaxY(_textView.frame) + 10, 100, 30);
    }
    else{
    
        _label4.frame = CGRectMake(10, CGRectGetMaxY(_textView.frame), 100, 30);
    }
    [_mainView addSubview:_label4];
    
//    UIView * view4 = [GDFactory creatUIViewWithFrame:CGRectMake(10, CGRectGetMaxY(_label4.frame), SCR_W - 20, SCR_H/5) backColor:[UIColor whiteColor]];
//    if (iPhone4) {
//        view4.frame = CGRectMake(10, CGRectGetMaxY(_label4.frame) + 5, SCR_W - 20, SCR_H/5);
//    }else{
//    
//        view4.frame = CGRectMake(10, CGRectGetMaxY(_label4.frame), SCR_W - 20, SCR_H/5);
//    }
//    [view4.layer setBorderWidth:1.0];
//    [view4.layer setBorderColor:RGBA(202, 202, 202, 1.0).CGColor];
//    [_mainView addSubview:view4];
    
    // 图片选择
//    _addImageView = [[GYLAddImage alloc] initWithFrame:CGRectMake(0, 0, view4.frame.size.width, view4.frame.size.height)];
    
    _addImageView = [[GYLAddImage alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_label4.frame), SCR_W - 20, SCR_H/5)];
    [_addImageView.layer setBorderColor:RGBA(202, 202, 202, 1.0).CGColor];
    [_addImageView.layer setBorderWidth:1.0];
    _addImageView.backgroundColor = [UIColor whiteColor];
    
    __weak typeof(self) weakSelf = self;
    __weak XYTextView *weakTextView = _textView;
    
    _addImageView.textViewBlock = ^(){
        [weakTextView resignFirstResponder];
    };
    _addImageView.block = ^(UIViewController *pickerVC){
        [weakSelf presentViewController:pickerVC animated:YES completion:nil];
    };
    if (_addImageView.images.count >= 5) {
        _addImageView.frame = CGRectMake(10, CGRectGetMaxY(_label4.frame), SCR_W - 20, SCR_H/5 * 2);
    }
    [_mainView addSubview:_addImageView];
    
    
    //  下单按钮
    _grabBtn = [[UIButton alloc] init];
    _grabBtn.frame = CGRectMake(SCR_W/4, CGRectGetMaxY(_addImageView.frame)+30, SCR_W/2, 40);
//    if (iPhone4) {
//        _grabBtn.frame = CGRectMake(0, 0, SCR_W/2, 30);
//        _grabBtn.center = CGPointMake(SCR_W * 0.5, CGRectGetMaxY(_addImageView.frame)+ 30);
//    }else{
//    
//        _grabBtn.frame = CGRectMake(0, 0, SCR_W/2, 40);
//        _grabBtn.center = CGPointMake(SCR_W * 0.5, CGRectGetMaxY(_addImageView.frame)+ 30);
//    }
    
    [_grabBtn setBackgroundImage:[UIImage imageNamed:@"3在线下单"] forState:UIControlStateNormal];
    [_grabBtn setTitle:@"我要下单" forState:UIControlStateNormal];
    [_grabBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _grabBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_grabBtn addTarget:self action:@selector(clickGet) forControlEvents:UIControlEventTouchUpInside];
    
    [_mainView addSubview:_grabBtn];
    _mainView.contentSize = CGSizeMake(SCR_W, CGRectGetMaxY(_grabBtn.frame)+80);
    
}

- (void)doneClick {
    CGRect textViewFrame = _textView.frame;
    [_textView resignFirstResponder];
    _textView.frame = textViewFrame;
}


#pragma mark -- 请求区位数据
- (void)requestWGHData{

    [_roomArray removeAllObjects];
    [_cateArray removeAllObjects];
    [_dataArray removeAllObjects];
    
    NSArray * keyArray = @[@"MODULE",@"USER_ID",@"USER_CODE",@"GUID",@"PARAMETER1"];
    NSArray * valueArray = @[@"WGH",@"67196",@"00086554",@"",@""];
    NSDictionary * dict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,GetWGH_URL];
//    [SVProgressHUD showWithStatus:@"加载中..."];
    
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        for (NSDictionary * dataDict in array) {
            
            GetWGHModel * model = [[GetWGHModel alloc] initWithDict:dataDict];
            [_roomArray addObject:model];
        }
        [_roomTableView reloadData];
        
//        [SVProgressHUD dismiss];
    } withTag:101];
//    _roomArray = [[NSMutableArray alloc] initWithObjects:@"北京",@"天津",@"青岛",@"上海", nil];
//    _cateArray = [[NSMutableArray alloc] initWithObjects:@"北京",@"天津",@"青岛",@"上海",nil];
    
    
}

#pragma mark -- 请求订单类型数据
- (void)requestSERVICEData{

    [_roomArray removeAllObjects];
    [_cateArray removeAllObjects];
    [_dataArray removeAllObjects];
    
    NSArray * keyArray = @[@"MODULE",@"USER_ID",@"USER_CODE",@"GUID",@"PARAMETER1"];
    NSArray * valueArray = @[@"SERVICE",@"1219",@"00086554",@"",@""];
    NSDictionary * dict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,GetWGH_URL];
//    [SVProgressHUD showWithStatus:@"加载中..."];
    
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        for (NSDictionary * dataDict in array) {
            
            GetWGHModel * model = [[GetWGHModel alloc] initWithDict:dataDict];
            [_cateArray addObject:model];
        }
        [_cateTableView reloadData];
        
//        [SVProgressHUD dismiss];
    } withTag:101];
}

#pragma mark -- 我要下单数据操作
- (void)RequestOrderSendData{
    [_textView resignFirstResponder];
    [_roomTableView reloadData];
    [_cateTableView reloadData];
    // 获取系统时间
//    NSDate * date = [NSDate date];
//    NSTimeZone * zone = [NSTimeZone systemTimeZone];
//    NSInteger interval = [zone secondsFromGMTForDate: date];
//    // 获取当前时间
//    NSDate * localeDate = [date  dateByAddingTimeInterval: interval];
    
    // 取出数据
    NSUserDefaults * roomDefaults = [NSUserDefaults standardUserDefaults];
    NSString * guid = [roomDefaults objectForKey:@"GRID_GUID"];
    NSString * type = [roomDefaults objectForKey:@"TYPE"];
    
    // 问题描述
    NSString * remark = _textView.text;
//    NSString * remarkStr = [remark stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString * OBJECT_GUID = @"86d92072-9021-409b-b9db-eb0001c91986";
    
    NSArray * keyArray = @[@"GRID_GUID",@"ORDER_TYPE",@"OBJECT_GUID",@"REMARK",@"USER_ID",@"USER_CODE",@"ATTACHMENT"];
    NSArray * valueArray = @[guid,type,OBJECT_GUID,remark,USER_ID,USER_CODE,@""];
    NSDictionary * dict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,OrderSend_URL];
//    [SVProgressHUD showWithStatus:@"加载中..."];
    
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        for (NSDictionary * dataDict in array) {
            _state = [dataDict objectForKey:@"STATUS"];
            _ID = [dataDict objectForKey:@"ID"];
        }
        
        if ([_state isEqualToString:@"SUCCESS"]) {
            [MBProgressHUD HUDShowMessage:@"下单成功" addedToView:self.view];
            
            _roomLabel.text = @"请选择";
            [_cateButton setTitle:@"请选择" forState:UIControlStateNormal];
            [_textView clearText];
            [self textViewDidChange:_textView];
            [_addImageView clearAllImage];
            
        }else{
            [MBProgressHUD HUDShowMessage:@"下单失败" addedToView:self.view];
        }
        
        // 上传图片
        [self upLoadImage];
        
        [_roomTableView reloadData];
        [_cateTableView reloadData];
    } withTag:101];
}

#pragma mark -- 上传图片

- (void)upLoadImage{

    NSArray * keyArray = @[@"ID",@"MODULE",@"ATTACHMENT"];
    
    NSMutableArray * imageArray = _addImageView.imageDataArr;
    
    if (imageArray.count > 0) {
        
    
    for (NSData * imageData in imageArray) {
        
        NSString * imageStr = [imageData base64Encoding];
        NSArray * valueArray = @[_ID,@"INSPECTION",imageStr];
        NSDictionary * dict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
        NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,UploadAttachment_URL];
        
        HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
        [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
            NSMutableArray * array = [http.returnArray mutableCopy];
//            for (NSDictionary * dataDict in array) {
//                _imageState = [dataDict objectForKey:@"ID"];
//            }
        } withTag:101];
        
      }
    }
}

#pragma mark -- 搭建区位列表
- (void)buildRoomTableView{

    if (!_roomTableView) {
        _roomTableView = [[UITableView alloc] init];
        _roomTableView.rowHeight = 30;
//        _roomTableView.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame), _view1.frame.size.width, SCR_H - CGRectGetMaxY(_view1.frame));
        _roomTableView.frame = CGRectMake(10, CGRectGetMaxY(_view1.frame), _view1.frame.size.width, 150);
        _roomTableView.delegate = self;
        _roomTableView.dataSource = self;
        _roomTableView.tag = 100;
        _roomTableView.tableFooterView = [[UIView alloc] init];
        //    _roomTableView.backgroundColor = [UIColor redColor];
        [_mainView addSubview:_roomTableView];
    }
}

#pragma mark -- 搭建订单分类列表
- (void)buildCateTableView{
    
    if (!_cateTableView) {
        _cateTableView = [[UITableView alloc] init];
        _cateTableView.rowHeight = 30;
//        _cateTableView.frame = CGRectMake(10, CGRectGetMaxY(_view2.frame), _view2.frame.size.width, SCR_H - CGRectGetMaxY(_view2.frame) - 120);
        _cateTableView.frame = CGRectMake(10, CGRectGetMaxY(_view2.frame), _view2.frame.size.width, 150);
        _cateTableView.delegate = self;
        _cateTableView.dataSource = self;
        _cateTableView.tag = 101;
        _cateTableView.tableFooterView = [[UIView alloc] init];
        //    _cateTableView.backgroundColor = [UIColor redColor];
        [_mainView addSubview:_cateTableView];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (tableView.tag == 100) {
        return _roomArray.count;
    }
    else{
    
        return _cateArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString * ID = @"GD";
    if (tableView.tag == 100) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        GetWGHModel * roomModel = _roomArray[indexPath.row];
        cell.textLabel.text = roomModel.TEXT;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        return cell;
    }else{
    
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        GetWGHModel * cateModel = _cateArray[indexPath.row];
        cell.textLabel.text = cateModel.TEXT;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        return cell;

        
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [_roomTableView removeFromSuperview];
    _roomTableView = nil;
    [_cateTableView removeFromSuperview];
    _cateTableView = nil;
    roomShow = NO;
    cateShow = NO;
    NSUserDefaults * roomDefaults = [NSUserDefaults standardUserDefaults];
    
    if (tableView.tag == 100) {

        GetWGHModel * roomModel = _roomArray[indexPath.row];
        _roomLabel.text = roomModel.TEXT;
        
        // 保存数据
        
        NSString * guid = roomModel.GUID;
        
        [roomDefaults setObject:guid forKey:@"GRID_GUID"];
        
    }
    if (tableView.tag == 101) {
        
        GetWGHModel * cateModel = _cateArray[indexPath.row];
        [_cateButton setTitle:cateModel.TEXT forState:UIControlStateNormal];
        
         // 保存数据
        NSString * type = cateModel.GUID;
        [roomDefaults setObject:type forKey:@"TYPE"];
//        NSString * str = [roomDefaults objectForKey:@"TYPE"];
//        NSLog(@"%@",str);
        
    }
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [_cateTableView removeFromSuperview];
    _cateTableView = nil;
    [_roomTableView removeFromSuperview];
    _roomTableView = nil;
    CGRect textViewFrame = _textView.frame;
    [_textView resignFirstResponder];
    _textView.frame = textViewFrame;
}

#pragma mark -- UITextFieldDelegate
- (void)textViewDidChange:(UITextView *)textView {
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    if (size.height < SCR_H/8) {
        _textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, _textView.frame.size.width, SCR_H/8);
    } else {
        _textView.frame = CGRectMake(_textView.frame.origin.x, _textView.frame.origin.y, _textView.frame.size.width, size.height);
    }
    _textView.countNumLabel.frame = CGRectMake(CGRectGetMaxX(_textView.frame)-110, CGRectGetMaxY(_textView.frame)+6, 100, 21);
    _label4.frame = CGRectMake(10, CGRectGetMaxY(_textView.frame), 100, 30);
    _addImageView.frame = CGRectMake(10, CGRectGetMaxY(_label4.frame), SCR_W - 20, SCR_H/5);
    _grabBtn.frame = CGRectMake(SCR_W/4, CGRectGetMaxY(_addImageView.frame)+30, SCR_W/2, 40);
    _mainView.contentSize = CGSizeMake(SCR_W, CGRectGetMaxY(_grabBtn.frame)+80);
}



#pragma mark -- 点击下单
- (void)clickGet{
    if ([_roomLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD HUDShowMessage:@"请选择订单区位" addedToView:self.view];
        return;
    }
    if ([_cateButton.titleLabel.text isEqualToString:@"请选择"]) {
        [MBProgressHUD HUDShowMessage:@"请选择订单类型" addedToView:self.view];
        return;
    }
    NSString * remark = _textView.text;
    if ([remark isEqualToString:@""]) {
        [MBProgressHUD HUDShowMessage:@"请填写描述" addedToView:self.view];
        return;
    }
    [self alertError:@"确认下单？"];

}

#pragma mark - 弹框提示
- (void)alertError:(NSString *)error{
    
    // 1.弹框
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:error delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        [self RequestOrderSendData];
        
    }
}

#pragma mark -- 点击位置区位
- (void)roomGseture{
    if (!roomShow) {
        [self buildRoomTableView];
    } else {
        [_cateTableView removeFromSuperview];
        _cateTableView = nil;
        [_roomTableView removeFromSuperview];
        _roomTableView = nil;
    }
    roomShow = !roomShow;
}

#pragma mark --- 点击订单类型
- (void)cateGesture{
    if (!cateShow) {
        [self buildCateTableView];
    } else {
        [_cateTableView removeFromSuperview];
        _cateTableView = nil;
        [_roomTableView removeFromSuperview];
        _roomTableView = nil;
    }
    cateShow = !cateShow;
}

@end
